package cn.wzy.service.impl;

import cn.wzy.enums.AppHttpCodeEnum;
import cn.wzy.mapper.PermissionMapper;
import cn.wzy.pojo.Permission;

import cn.wzy.results.ResponseResult;
import cn.wzy.service.PermissionService;
import cn.wzy.vo.GoPageVo;
import cn.wzy.vo.PageVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * (Permission)表服务实现类
 *
 * @author makejava
 * @since 2022-12-12 19:57:25
 */
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class PermissionServiceImpl extends ServiceImpl<PermissionMapper,Permission>implements PermissionService {
    @Resource
    private PermissionMapper permissionMapper;

    /*
    * 权限资源分页
    * */
    @Override
    public ResponseResult permissionPage(GoPageVo goPageVo) {
//        分页
        Long stater = (goPageVo.getPageIndex()-1) * goPageVo.getPageSize();
        List<Permission> permissionList = permissionMapper.permissionPage(goPageVo.getKeyword(),stater,goPageVo.getPageSize());
//        总数
        Long total = permissionMapper.getTotal(goPageVo);
//        封装
        PageVo pageVo = new PageVo(permissionList, total, goPageVo.getPageSize());
        return ResponseResult.okResult(pageVo);
    }

    /*
    * 权限的修改/添加
    * */
    @Override
    @Transactional
    public ResponseResult savePermission(Permission permission) {
//        判断sn是否唯一
        Permission permissionSn = permissionMapper.getPermissionBySn(permission.getSn());
        if (!Objects.isNull(permissionSn)){
//           不唯一
            return ResponseResult.errorResult(AppHttpCodeEnum.PERMISSION_SN);
        }
//        判断id来确定是否是添加
        if (Objects.isNull(permission.getId())){
//            添加
            permissionMapper.addPermission(permission);
            return ResponseResult.okResult();
        }
//        修改
        permissionMapper.updatePermission(permission);
        return ResponseResult.okResult();
    }

    /*
    * 删除单个资源
    * */
    @Override
    @Transactional
    public ResponseResult deletePermission(Long id) {
//        判断是否有子资源
        List<Permission> permissionList = permissionMapper.getPermissionLower(id);
        if (Objects.isNull(permissionList)||permissionList.size()<1){
//            没有子资源
//            可以删除
            permissionMapper.deletePermission(id);
            return ResponseResult.okResult();
        }
//        有子资源不可以删除
        return ResponseResult.errorResult(AppHttpCodeEnum.PERMISSION_DELETE);
    }

    /*
    * 批量删除
    * */
    @Override
    @Transactional
    public ResponseResult deletePermissionList(List<Long> ids) {
//        获取到所有ids
        List<Permission> permissionList = permissionMapper.getPermissionList(ids);
//        判断每个是否有子资源
        for (Permission permission : permissionList) {
            List<Permission> permissionLower = permissionMapper.getPermissionLower(permission.getId());
            if (!Objects.isNull(permissionLower)&&permissionLower.size()>0){
//                有子资源
                return ResponseResult.errorResult(AppHttpCodeEnum.PERMISSION_DELETE);
            }
        }
//        删除
        permissionMapper.deletePermissionList(ids);
        return ResponseResult.okResult();
    }

    /*
    * 获取一级权限
    * */
    @Override
    public ResponseResult getPermissionParent() {
        List<Permission> permissionList = permissionMapper.getPermissionParent();
        return ResponseResult.okResult(permissionList);
    }

    /*
    * 获取权限数
    * */
    @Override
    public ResponseResult getPermissionTree() {
        List<Permission> permissionList = permissionMapper.getPermissionTree();
        return ResponseResult.okResult(permissionList);
    }

    /*
    * 获取所有SN
    * */
    @Override
    public ResponseResult getAllSn() {
        List<String> sns = permissionMapper.getPermissionAllSn();
        return ResponseResult.okResult(sns);
    }
}
